package cn.quicklydoit.resource;

import io.quarkus.mailer.Mail;
import io.quarkus.mailer.MailTemplate;
import io.quarkus.mailer.Mailer;
import io.quarkus.mailer.reactive.ReactiveMailer;
import io.quarkus.qute.CheckedTemplate;
import io.smallrye.common.annotation.Blocking;
import io.smallrye.mutiny.Uni;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;

import javax.inject.Inject;
//import javax.validation.Valid;
//import javax.validation.constraints.Email;
//import javax.validation.constraints.NotBlank;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/mail")
@Tag(name = "MailResource",description="邮箱接口列表")
public class MailResource {


    @Inject
    Mailer mailer;

    @Inject
    ReactiveMailer reactiveMailer;

    @GET
    @Path("send")
    @Blocking
    @Operation(summary="发送邮件",description = "发送邮件测试接口")
    public Uni<Void> sendEmail(){

       return reactiveMailer.send(Mail.withHtml("cym1358723682@163.com","测试","111123232"));

    }

    @GET
    @Blocking
    public void sendEmaila() {
        mailer.send(
                Mail.withText("cym1358723682@163.com",
                        "Ahoy from Quarkus",
                        "A simple email sent from a Quarkus application."
                )
        );
    }

    @GET
    @Path("/sendHtml")
    @Operation(summary = "发送邮件",description = "测试发送邮件接口，使用模板")
    @Produces(MediaType.TEXT_PLAIN)
    public Uni<Response> sendEmail(@QueryParam("email") String email,
                                   @QueryParam("name") String name){
        String code="111222";
        return Templates.validCode(code)
                .to(email)
                .subject("Ahoy " + name + "!")
                .data("validCode",code)
                .send()
                .map(x -> Response.accepted().build());
    }


    @CheckedTemplate
    static class Templates {
        public static native MailTemplate.MailTemplateInstance validCode(String validCode);
    }

}
